/**
 * Created by zxd on 2017/11/1.
 */

$(function(){
    //加载数据
    loadDatas();


})


//加载数据

function loadDatas(){

    //$.ajax 请求数据
    $.ajax({
        type:"post",
        data:{} ,
        url:"../../../php/sys/admin/loadAdminList.php",
        beforeSend: function(){
            $("#load").css("display","block");
        },
        success:function(msg){
            $("#load").css("display","none");
            var datas = msg.records;
            //console.log(msg.records);
            //return;
            var html = "";
            //表格数据
            if(datas.length==0){
                html += "<tr><td colspan='3'>没查询到符合条件的数据~</td></tr>";
            }else{
                for(var i=0;i<datas.length;i++){
                    html+="<tr>";
                    html += "<td>"+datas[i].userid+"</td>";
                    html += "<td>"+datas[i].nickname+"</td>";
                    html += "<td>"+datas[i].account+"</td>";
                    html += "<td>"+datas[i].password+"</td>";
                    html += "</tr>";
                }
            }

            $("#databody").html(html);

        },
        error:function(msg){
            alert("连接出错！");
        }
    });
}

//点击添加按钮
function subAdminAdd(){
    //账号
    var account = $("#account").val();
    //密码
    var password = $("#password").val();
    //如果账号为空
    if(account==""){
        $("#account").css("border","1px solid red");
       // $("#sError").css("display","block");
    }else {
        $("#account").css("border","");
        //$("#sError").css("display","none");
    }
    if(password==""){
        $("#password").css("border","1px solid red");
       // $("#sError").css("display","block");
    }else {
        $("#password").css("border","");
        //$("#sError").css("display","none");
    }



    //账号H和密码不为空
    if(account!="" && password!=""){
        $.ajax({
            type:"post",
            data:{"account":account,"password":password},
            url:"../../../php/sys/admin/addAdmin.php",
            beforeSend:function(){
                $("#load").css("display","block");
            },
            success:function(msg){
                $("#load").css("display","none");
                if(msg=="1"){   //添加成功
                    $("#addAdmin").modal("hide");
                    $("#tishi").modal("show");
                    $("#load").css("display","block");
                }else {
                   alert(msg);
                }
            }

        })
    }


}



//取消管理员添加弹框
function cancelAdminAdd(){
    $("#account, #password").css("border","");
    $("#dtError, #sError").css("display","none");
    $("#addAdmin").modal("hide");
}